package com.jucai.pppeiyouclient.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.AnimUtil;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.TextCheckUtils;
import com.jucai.pppeiyouclient.utils.ToastUtils;

import static com.jucai.pppeiyouclient.R.id.common_title_text;


public class BrowserActivity extends BaseActivity {

    private WebView mainWebView;
    private TextView titleTextView;
    private ContentLoadingProgressBar mainProgressBar;

    private String urlString;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_browser);

        mainWebView = (WebView) findViewById(R.id.main_web_view);
        titleTextView = (TextView) findViewById(common_title_text);
        mainProgressBar = (ContentLoadingProgressBar) findViewById(R.id.main_progressB_bar);

        mainWebView.setHorizontalScrollBarEnabled(false);
        mainWebView.setVerticalScrollBarEnabled(false);
        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setSupportZoom(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);

        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);

    }

    @Override
    protected void initData() {

        LogUtils.d("intiData");

        urlString = getIntent().getStringExtra("url");

        if (TextCheckUtils.isEmpty(urlString)) {
            ToastUtils.showToast("数据错误");
            finish();
            return;
        }

        mainWebView.loadUrl(urlString);

    }

    @Override
    protected void loadNetData() {

    }

    @Override
    protected void setListener() {

        mainWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }
        });

        mainWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int progress) {
                mainProgressBar.setProgress(progress);
                if (progress == 100) {
                    if (mainProgressBar.getVisibility() == View.VISIBLE) {
                        AnimUtil.goneAlpha(mainProgressBar);
                    }
                    mainProgressBar.setVisibility(View.GONE);
                } else {
                    if (mainProgressBar.getVisibility() == View.GONE) {
                        AnimUtil.showAlpha(mainProgressBar);
                    }
                    mainProgressBar.setVisibility(View.VISIBLE);
                }
                super.onProgressChanged(view, progress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                titleTextView.setText(title);
            }
        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;
            case R.id.common_home_back:
                startActivity(new Intent(BrowserActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;
        }

    }

}
